<?php

namespace App\Store\Models\Member;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Common\Services\Date\TimerRange;
use Illuminate\Database\Eloquent\Casts\Attribute;

class SiteMemberConsumeRecord extends Model
{
    protected $table = 'site_member_consume_record';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getlist(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);

        //查询列表有 cp_id,则导入时,需要写入。删除字段是否需要，需要也需要筛选
        $query = self::where(['cp_id' => $cp_id])->orderBy('id', 'desc');

//        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
//        if($store_ids){//权限控制
//            if(is_array($store_ids)){//获取当前权限下的门店
//                $query->whereIn('store_id',$store_ids);
//            }
//            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
//                $query->where('store_id', intval($store_ids));
//            }
//        }
        /**
         *  goods_sn_code,mobile,order_sn,orderend_time,orderstart_time,points_current_max,points_current_min
         *  real_name,store_id
         *
         * */
        $goods_sn_code = $request->goods_sn_code;
        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', $goods_sn_code);
        }
        $mobile = $request->mobile;
        if ($mobile != '') {
            $query->where('mobile', $mobile);
        }
        $order_sn = $request->order_sn;
        if ($order_sn != '') {
            $query->where('order_sn', $order_sn);
        }
        if (!empty($request->orderend_time) && !empty($request->orderstart_time)) {
            $orderend_time = strtotime($request->orderend_time);
            $orderstart_time = strtotime(sprintf('%s %s', $request->orderstart_time, '23:59:59'));
            $query->whereBetween('order_at', [$orderstart_time, $orderend_time]);
        }
        if (!empty($request->points_current_min) && !empty($request->points_current_max)) {
            $query->whereBetween('points', [$request->points_current_min, $request->points_current_max]);
        }
        $mem_name = $request->mem_name;
        if ($mem_name != '') {
            $query->where('mem_name', 'like', '%' . $mem_name . '%');
        }
        $store_name = $request->store_name;
        if ($store_name != '') {
            $query->where('store_name', 'like', '%' . $store_name . '%');
        }

        if ($request->isdoexport == 1) {
            $limit = $request->limit ?? 5000;
            $list = $query->paginate($limit, $request->columns, 'page', $request->nowPage)->toArray();
            //$count = $query->count();
            return ['list' => $list];
        }
        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    protected function isReplace():Attribute
    {
        return Attribute::make(
            get: function($value){
                if($value == '2'){
                    return '是';
                }else{
                    return '否';
                }
            }
        );
    }
    protected function orderAt():Attribute
    {
        return Attribute::make(
            get: function($value){
                if($value == 0){
                    return '';
                }else{
                    return date('Y-m-d',$value);
                }
            }
        );
    }
    //商品标价  标价 回收金额  三个字段前端没有 /100，后端处理
    protected function goodsPrice():Attribute
    {
        return Attribute::make(
            get: function($value){
                if($value != 0){
                    return $value /100;
                }
            }
        );
    }
    protected function price():Attribute
    {
        return Attribute::make(
            get: function($value){
                if($value != 0){
                    return $value /100;
                }
            }
        );
    }
    protected function recoveryMoney():Attribute
    {
        return Attribute::make(
            get: function($value){
                if($value != 0){
                    return $value /100;
                }
            }
        );
    }
}
